import get1 from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import hide from '@dfeidao/atom-web/ui/hide';


export default async function a005(fd: IFeidaoAiBrowserComponent) {
	// todo
	// 点击保存按钮时
	// zj-004 得到的接收人和总数
	const arr = get(fd, 'arr');
	const d = get(fd, 'd') as number;
	// console.log('arr', arr);
	// console.log('d', d);
	if (d === undefined) {
		error('请选择接收人');
		return;
	}
	// 标题;
	const inform_title1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="inform_title"]');
	const inform_title2 = inform_title1.value;
	if (!inform_title2) {
		error("请输入标题");
		return;
	}
	// 内容
	const inform_content1 = fd.data.node.querySelector<HTMLTextAreaElement>('[ data-id="inform_content"]');
	const inform_content2 = inform_content1.value;
	if (!inform_content2) {
		error("请输入内容");
		return;
	}
	// 登录人的姓名和编号和产品ID
	const staff_no1 = get1('a');
	const staff_name1 = get1('name');
	const productid1 = get1('productid');
	const res = await nodejs<{ dt: { result: string } }>('notification-notice/zj-000004/s003', {});
	const inform_no1 = res.dt.result;
	// 全局编码accepter_no
	const res1 = await nodejs<{ dt: { result: string } }>('notification-notice/zj-000004/s006', {});
	const accepter_no1 = res1.dt.result;
	// 系统时间
	const notification_time1 = await nodejs<{}>('notification-notice/zj-000004/s004', {});
	// 添加通知公告notice>
	const res2 = await nodejs<{ code: 0 | 1; result: string; }>('notification-notice/zj-000004/s005', {
		filter: {
			inform_no: inform_no1,
			inform_title: inform_title2,
			inform_content: inform_content2,
			notification_time: notification_time1,
			staff_no: staff_no1,
			staff_name: staff_name1,
			productid: productid1
		}
	});
	if (res2.code === 1) {
		let a = 0;
		for (let i = 0; i < d; i++) {
			const res3 = await nodejs<{ code: 0 | 1; result: string; }>('notification-notice/zj-000004/s007', {
				filter: {
					accepter_no: accepter_no1,
					pub_user_no: arr[i].staff_no,
					pub_user_name: arr[i].staff_name,
					department_no: "",
					dept_name: "",
					inform_no: inform_no1,
					productid: productid1
				}
			});
			if (res3.code === 1) {
				a++;
			}
		}
		// console.log('a', a);
		if (a === d) {
			alert('添加成功！');
			fire('zj-000001', 'a003');
			hide(fd.data.node);
		} else {
			alert('通知公告添加成功,接受人添加失败！');
		}
	} else {
		alert('通知公告添加失败！');
	}
	// arr = Array();
	// d = 0;
	set(fd, 'arr', arr);
	set(fd, 'd', d);
}
